package org.kl.bf.utils;

import java.io.File;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author hqm
 * @CreateTime 2014年6月11日 下午8:45:45
 */
public class KLFileUtils {
	private static Logger logger = LoggerFactory.getLogger(KLFileUtils.class);

	public static String getExcelImportFileSavePath(String configFilePath, String realPath) throws Exception {
		String saveFolder = configFilePath;
		if (!saveFolder.endsWith("/")) {
			saveFolder += "/";
		}
		// 如果配置路径不以“/”开头，即不是linux绝对路径，或者不是windows绝对路径，则表示在项目路径下
		if (!saveFolder.startsWith("/") && saveFolder.indexOf(":") == -1) {
			saveFolder = realPath + "/" + saveFolder;
		}
		// 检查目录
		File uploadDir = new File(saveFolder);
		if (!uploadDir.exists()) {
			uploadDir.mkdirs();
			logger.info("目录" + uploadDir + "不存在，现已创建");
		}
		// 检查目录写权限
		if (!uploadDir.canWrite()) {
			throw new Exception("上传目录[" + saveFolder + "]没有写权限。");
		}
		// 创建文件夹(每天一个文件夹)
		saveFolder += (new DateTime().toString("yyyy-MM")) + "/";
		uploadDir = new File(saveFolder);
		if (!uploadDir.exists()) {
			uploadDir.mkdirs();
			logger.info("目录" + uploadDir + "不存在，现已创建");
		}
		return saveFolder;
	}
	
	
	public static String getFileSavePath(String realPath) throws Exception {
		String saveFolder=realPath;
		if (!saveFolder.endsWith("/")) {
			saveFolder += "/static/image/";
		}
		// 如果配置路径不以“/”开头，即不是linux绝对路径，或者不是windows绝对路径，则表示在项目路径下
		/*if (!saveFolder.startsWith("/") && saveFolder.indexOf(":") == -1) {
			saveFolder = realPath + "/static/image/";
		}*/
		// 检查目录
		File uploadDir = new File(saveFolder);
		if (!uploadDir.exists()) {
			uploadDir.mkdirs();
			logger.info("目录" + uploadDir + "不存在，现已创建");
		}
		// 检查目录写权限
		if (!uploadDir.canWrite()) {
			throw new Exception("上传目录[" + saveFolder + "]没有写权限。");
		}
		// 创建文件夹(每天一个文件夹)
		saveFolder += (new DateTime().toString("yyyy-MM")) + "/";
		uploadDir = new File(saveFolder);
		if (!uploadDir.exists()) {
			uploadDir.mkdirs();
			logger.info("目录" + uploadDir + "不存在，现已创建");
		}
		return saveFolder;
	}
}
